package stackdemo;
import java.util.Arrays;
public class MyStack {
    public int[] elem;
    public int usedSize;
    public static final int DEFAULT_CAPACITY = 10;
    public MyStack() {
        this.elem = new int[DEFAULT_CAPACITY];
    }
    /**
     * 压栈,入栈
     */
    public void push(int val) {
        //判满
        if(isFull()) {
            elem = Arrays.copyOf(elem,2 * elem.length);
        }
        //添加
        elem[usedSize++] = val;
    }
    private boolean isFull() {
        return usedSize == elem.length;
    }
    /**
     * 出栈,并删除
     */
    public int pop() {
        //判空
        if(isEmpty()) {
            throw new StackEmptyException("栈为空");
        }
        //删除
        int ret = elem[usedSize - 1];
        usedSize--;
        return ret;
    }
    public boolean isEmpty() {
        return usedSize == 0;
    }
    /**
     * 出栈,不删除
     */
    public int peek() {
        //判空
        if(isEmpty()) {
            throw new StackEmptyException("栈为空");
        }
        //删除
        int ret = elem[usedSize - 1];
        return ret;
    }
}
